package cn.hx.controller;

import cn.hx.entity.Education;
import cn.hx.entity.National;
import cn.hx.entity.User;
import cn.hx.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class StaffController {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private EducationService educationService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private NationalService nationalService;

    //    查询修改页面数据显示
    @GetMapping("searchUser")
    public ModelAndView getStaff(User user) {
        ModelAndView modelAndView = new ModelAndView("searchUser");
        modelAndView.addObject("users", userService.findAll(user));
        modelAndView.addObject("dept", deptService.findAll());
        return modelAndView;
    }

    //    新增页面数据显示
    @GetMapping("createUser")
    public ModelAndView getCreateUser() {
        ModelAndView modelAndView = new ModelAndView("createUser");
        modelAndView.addObject("dept", deptService.findAll());
        modelAndView.addObject("educations", educationService.findAll());
        modelAndView.addObject("project", projectService.findAll());
        modelAndView.addObject("nationals", nationalService.findAll());
        return modelAndView;
    }

    //    模糊查询and部门查询
    @PostMapping("searchUser")
    public ModelAndView postStaff(User user) {
        ModelAndView modelAndView = new ModelAndView("searchUser");
        modelAndView.addObject("users", userService.findAll(user));
        modelAndView.addObject("dept", deptService.findAll());
        return modelAndView;
    }

    //    删除
    @GetMapping("deleteUser/{id}")
    public String getDeleteUser(@PathVariable Integer id) {
        userService.deleteById(id);
        return "redirect:/searchUser";
    }

    //    修改回填
    @GetMapping("updateUser/{id}")
    public String getUpdateUser(@PathVariable Integer id, Model model) {
        User user = userService.queryById(id);
        model.addAttribute("user", user);
        System.out.println(user);
        model.addAttribute("dept", deptService.findAll());
        model.addAttribute("educations", educationService.findAll());
        model.addAttribute("project", projectService.findAll());
        model.addAttribute("nationals", nationalService.findAll());
        return "updateUser";
    }

    //    修改
    @PostMapping("editUser/{id}")
    public String postSaveUser(@PathVariable Integer id, User user) {
        user.setId(id);
        userService.update(user);
        return "redirect:/searchUser";
    }

    //    新增
    @PostMapping("saveUser")
    public String postSaveUser(User user) {
        user.setNumber(String.valueOf(System.currentTimeMillis()));
        user.setRoleId(1);
        userService.insert(user);
        System.out.println(user);
        return "redirect:/searchUser";
    }

}
